package com.eleventwelve.iris.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.opentides.bean.SystemCodes;
import org.opentides.editor.BaseEntityEditor;
import org.opentides.service.SystemCodesService;
import org.opentides.util.StringUtil;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.eleventwelve.iris.entity.Allergen;
import com.eleventwelve.iris.entity.Disease;
import com.eleventwelve.iris.entity.Patient;
import com.eleventwelve.iris.service.AllergenService;
import com.eleventwelve.iris.service.ContactNumberService;
import com.eleventwelve.iris.service.DiseaseService;
import com.eleventwelve.iris.service.PatientAllergyService;
import com.eleventwelve.iris.service.PatientDiseaseService;
import com.eleventwelve.iris.service.PatientService;

@SuppressWarnings("deprecation")
public class PatientFormController extends SimpleFormController{
	private PatientService patientService;
	private SystemCodesService systemCodesService;
	private AllergenService allergenService;
	private DiseaseService diseaseService;
	private ContactNumberService contactNumberService;
	private PatientAllergyService patientAllergyService;
	private PatientDiseaseService patientDiseaseService;
	
	
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		
		Patient patient = new Patient();
		
		String action = request.getParameter("action");
		if(action.equalsIgnoreCase("update")){
			String id = request.getParameter("id");
			if(!StringUtil.isEmpty(id) && StringUtils.isNumeric(id)){
				patient = patientService.load(id);
			}
		}
		
		return patient;
	}
	
	@Override
	protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("genders", systemCodesService.findSystemCodesByCategory("GENDER"));
		map.put("numberTypes", systemCodesService.findSystemCodesByCategory("NUMBER_TYPE"));
		map.put("allergyStatus", systemCodesService.findSystemCodesByCategory("ALLERGY_STATUS"));
		map.put("diseaseStatus", systemCodesService.findSystemCodesByCategory("DISEASE_STATUS"));
		map.put("allergens", allergenService.findAll());
		map.put("diseases", diseaseService.findAll());
		
		return map;
	}
	
	@Override
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		binder.registerCustomEditor(SystemCodes.class, new BaseEntityEditor(systemCodesService));
		binder.registerCustomEditor(Allergen.class, new BaseEntityEditor(allergenService));
		binder.registerCustomEditor(Disease.class, new BaseEntityEditor(diseaseService));
	}
	
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		
		Patient patient = (Patient) command;
		
		contactNumberService.savePatientContactNumbers(patient);
		
		patientAllergyService.savePatientAllergies(patient);
		
		patientDiseaseService.savePatientDisease(patient);
		
		patientService.save(patient);
		
		
		return new ModelAndView("redirect:/patient.jspx?id="+patient.getId()+"&action=update&item=Patient");
	}

	public PatientService getPatientService() {
		return patientService;
	}

	public void setPatientService(PatientService patientService) {
		this.patientService = patientService;
	}

	public SystemCodesService getSystemCodesService() {
		return systemCodesService;
	}

	public void setSystemCodesService(SystemCodesService systemCodesService) {
		this.systemCodesService = systemCodesService;
	}

	public AllergenService getAllergenService() {
		return allergenService;
	}

	public void setAllergenService(AllergenService allergenService) {
		this.allergenService = allergenService;
	}

	public DiseaseService getDiseaseService() {
		return diseaseService;
	}

	public void setDiseaseService(DiseaseService diseaseService) {
		this.diseaseService = diseaseService;
	}

	public ContactNumberService getContactNumberService() {
		return contactNumberService;
	}

	public void setContactNumberService(ContactNumberService contactNumberService) {
		this.contactNumberService = contactNumberService;
	}

	public PatientAllergyService getPatientAllergyService() {
		return patientAllergyService;
	}

	public void setPatientAllergyService(PatientAllergyService patientAllergyService) {
		this.patientAllergyService = patientAllergyService;
	}

	public PatientDiseaseService getPatientDiseaseService() {
		return patientDiseaseService;
	}

	public void setPatientDiseaseService(PatientDiseaseService patientDiseaseService) {
		this.patientDiseaseService = patientDiseaseService;
	}
	
}
